package org.blueoxygen.cimande2.ma.gene;

import java.util.List;

import org.blueoxygen.cimande2.ma.gene.adapter.PersonAdapter;
import org.blueoxygen.cimande2.ma.gene.entity.Person;
import org.blueoxygen.cimande2.ma.gene.manager.ConnectionManager;
import org.blueoxygen.cimande2.ma.gene.manager.DbManager;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class Child extends ListActivity {
	private int selectedItemId;
	private List<Person> childs;
	private ConnectionManager connectionManager;
	private Menu menu;
	private ProgressDialog myProgressDialog;
	String key, user;
	private DbManager dbManager;
	Person fromIntent;

	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.list);
		getListView().setClickable(true);
		getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		TextView listTitle = (TextView) findViewById(R.id.listTitle);
		listTitle.setText("   Child");
		ImageView imageView = (ImageView) findViewById(R.id.ImageView01);
		imageView.setBackgroundResource(R.drawable.usergrub);
		
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_title);
		
		connectionManager = new ConnectionManager();
		dbManager = new DbManager(this);
		Bundle bundle = getIntent().getExtras();
		fromIntent = (Person) bundle.getSerializable("myprofile");
		childs = connectionManager.getPersonList(dbManager.getConnectionUrl()
				+ "Cimande2/child/" + fromIntent.getId() + "/.json");
		PersonAdapter personAdapter = new PersonAdapter(this, childs);
		getListView().setAdapter(personAdapter);
		
		getListView().setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				selectedItemId = getListView().getCheckedItemPosition();
				Intent intent = new Intent(Child.this, Profile.class);
				Person person = new Person();
				person = connectionManager.getPersonalData(dbManager
						.getConnectionUrl()
						+ "Cimande2/person/" + getIdList(selectedItemId) + "/.json");
				intent.putExtra("myprofile", person);
				startActivity(intent);
			}
		});
		findViewById(R.id.homeBtn).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Child.this, DashBoard.class);
				startActivity(intent);
			}
		});
		findViewById(R.id.preferenceBtn).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Child.this, Preference.class);
				startActivity(intent);
			}
		});
	}

	private String[] getNameList() {
		String data[] = new String[childs.size()];
		int index = 0;
		for (Person c : childs) {
			data[index] = c.getFirstName();
			index++;
		}
		return data;
	}

	private String getIdList(int selectedIndex) {
		int index = 0;
		String data[] = new String[childs.size()];
		for (Person c : childs) {
			data[index] = c.getId();
			index++;
		}
		return data[selectedIndex];
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		this.menu = menu;
		addRegularMenuItems(menu);
		return true;
	}

	private void addRegularMenuItems(Menu menu) {
		int base = Menu.FIRST; // value is 1
		MenuItem addChild = menu.add(base, base, base, "Add Child");
		addChild.setIcon(R.drawable.add);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == 1) {
			myProgressDialog = ProgressDialog.show(Child.this, null,
					"Loading data...", true);
			new Thread() {
				@Override
				public void run() {
					try {
						Intent in = new Intent(Child.this, AddChild.class);
						in.putExtra("id", fromIntent.getId());
						startActivity(in);
						sleep(5000);
					} catch (Exception e) {
					}

					myProgressDialog.cancel();
				}
			}.start();

		}
		return true;
	}
}
